package com.example.dollarShipping.controller.auth;


import com.example.dollarShipping.entity.Carrier;
import com.example.dollarShipping.entity.Menu;
import com.example.dollarShipping.entity.User;
import com.example.dollarShipping.security.UserDetailsImpl;
import com.example.dollarShipping.service.CarrierService;
import com.example.dollarShipping.service.MenuService;
import com.example.dollarShipping.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class AuthController {

	@Resource
	UserService userService;

	@Resource
	CarrierService carrierService;
	@Resource
	private MenuService menuService;

	@GetMapping("/login")
	public String login(Model model) {
		return "auth/login";
	}

	@GetMapping("/error")
	public String fail(){
		return "auth/fail";
	}



	//得到用户列表
	@GetMapping("/userList")
	public String getUserList(Model model){
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext()
				.getAuthentication()
				.getPrincipal();
		List<Menu>  menu= menuService.getMenu(userDetails.getRoleId());
		model.addAttribute("menuList",menu);
		List<User> ul =  userService.getInfo(null);
		model.addAttribute("userList",ul);
		return "profile/userList";
	}
	@RequestMapping("/carrierList")
	public String showCarrierList(Model model) {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext()
				.getAuthentication()
				.getPrincipal();
		List<Menu>  menu= menuService.getMenu(userDetails.getRoleId());
		model.addAttribute("menuList",menu);
		List<Carrier> cl = carrierService.getInfo(null);
		model.addAttribute("carrierList",cl);
		return "carrier/carrierList";
	}

	@RequestMapping("/carrierDetail{carrierId}")
	public String showCarrierDetail(Model model, @PathVariable(name="carrierId") Long carrierId) {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext()
				.getAuthentication()
				.getPrincipal();
		List<Menu>  menu= menuService.getMenu(userDetails.getRoleId());
		model.addAttribute("menuList",menu);
		Carrier c = carrierService.findCarrier(carrierId);
		model.addAttribute("carrier",c);
		return "carrier/carrierInfo";
	}

}
